package com.example.iqiyitest.adapter;

import java.util.List;

import com.example.iqiyitest.R;
import com.example.iqiyitest.base.RecyclerBaseActivity;
import com.example.iqiyitest.info.MovieDetailInfo;
import com.example.iqiyitest.view.MovieView;
import com.example.iqiyitest.view.MovieView.MovieFocusChangeListener;

import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


@SuppressLint("HandlerLeak") 
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

	private MovieFocusChangeListener iconFocusChangeListener;
	private View.OnClickListener itemOnClickListener;
	private OnKeyListener itemOnKeyListener;
	private List<MovieDetailInfo> movies;
	int duration = 500;
	private RecyclerBaseActivity activity;

	public RecyclerAdapter(RecyclerBaseActivity activity, List<MovieDetailInfo> historys) {
		this.activity = activity;
		this.movies = historys;
	}

	public void setMovies(List<MovieDetailInfo> movies) {
		this.movies = movies;
	}

	@Override
	public int getItemCount() {
		return movies.size();
	}

	public List<MovieDetailInfo> getMovies() {
		return movies;
	}

	public void setIconFocusChangeListener(MovieFocusChangeListener iconFocusChangeListener) {
		this.iconFocusChangeListener = iconFocusChangeListener;
	}

	public void setItemOnClickListener(View.OnClickListener itemOnClickListener) {
		this.itemOnClickListener = itemOnClickListener;
	}
	
	public void setItemOnKeyListener(OnKeyListener itemOnKeyListener) {
		this.itemOnKeyListener = itemOnKeyListener;
	}

	@Override
	public void onBindViewHolder(ViewHolder arg0, int arg1) {
		MovieDetailInfo movie = movies.get(arg1);
		MovieView movieView = (MovieView) arg0.itemView;
		movieView.setIndex(arg1);
		movieView.setMovie(movie);
		movieView.setPosition(MovieView.POSITION_MIDDLE);
		if (arg1 == 0) {
			if (arg1 == (movies.size() - 1)) {
				movieView.setPosition(MovieView.POSITION_FIRST_AND_LAST);
			} else {
				movieView.setPosition(MovieView.POSITION_FIRST);
			}
		} else if (arg1 == (movies.size() - 1)) {
			movieView.setPosition(MovieView.POSITION_LAST);
		}
	}
	
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
		MovieView iconView = new MovieView(activity);
		iconView.setMovieFocusChangeListener(iconFocusChangeListener);
		iconView.setOnClickListener(itemOnClickListener);
		iconView.setOnKeyListener(itemOnKeyListener);
		return new ViewHolder(iconView);
	}

	public boolean remove(MovieView v) {
		if(v==null||v.getMovie()==null){
			return false;
		}
		MovieDetailInfo movie = (MovieDetailInfo) v.getMovie();
		int index = movies.indexOf(movie);
		if (index < 0) {
			return false;
		}
		activity.setFoucedView(null);
		if (index != 0) {
			if(v.focusSearch(View.FOCUS_LEFT)!=null&&v.focusSearch(View.FOCUS_LEFT) instanceof MovieView){
				MovieView leftView = (MovieView) v.focusSearch(View.FOCUS_LEFT);
				if (index == (movies.size()-1)) {
					if (index == 1) {
						leftView.setPosition(MovieView.POSITION_FIRST_AND_LAST);
					} else {
						leftView.setPosition(MovieView.POSITION_LAST);
					}
				}
				leftView.requestFocus();
			}
		} else if (movies.size() != 1) {
			if(v.focusSearch(View.FOCUS_RIGHT)!=null&&v.focusSearch(View.FOCUS_RIGHT) instanceof MovieView){
				MovieView rightView = (MovieView) v.focusSearch(View.FOCUS_RIGHT);
				if(movies.size()==2){
					rightView.setPosition(MovieView.POSITION_FIRST_AND_LAST);
				}else{
					rightView.setPosition(MovieView.POSITION_FIRST);
				}
				rightView.requestFocus();
			}
		}
		movies.remove(index);
		notifyItemRemoved(index);
		return true;
	}

	public void removeAll() {
		movies.clear();
		notifyDataSetChanged();
	}

	public class ViewHolder extends RecyclerView.ViewHolder {

		ImageView iv_poster;
		TextView tv_bak;

		public ViewHolder(View itemView) {
			super(itemView);
			iv_poster = (ImageView) itemView.findViewById(R.id.iv_poster);
			tv_bak = (TextView) itemView.findViewById(R.id.tv_bak);
		}

	}

}
